package pure.ui.manager 
{
	import pure.utils.DesT;
	import pure.ui.core.Component;
	import pure.ui.Panel;
	import pure.ui.skins.IBackgroundSkin;
	import pure.ui.skins.ISkin;
	import pure.ui.TipsItem;
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import pure.engine.core.SystemManager;
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair;
	/**
	 * 绝望Tips管理器
	 * @author Des
	 * 
	 * 使用：为宿主容器添加提示信息。
 	 * 显示data内容（推荐使用htmlText），data为回调方法或显示文本。
	 * （状态继承Tips后使用单例，并提供显示更新方法）

	 * TipsManager.add(owner,data)----添加，提供数据/方法。
	 * TipsManager.remove(owner)----删除。
	 * TipsManager.destroy()----回收
	 * 
	 * 使用：定点显示提示信息。
	 * TipsManager.showTipsAt(x,y,data,onShow)----定点显示
	 * TipsManager.clear()----清空
	 * 
	 */
	
public class TipsManager
{

	
	/* model */
	
	protected static var _tipsProps:Dictionary = new Dictionary(true);	// owner - data

	protected static var _enabled:Boolean = true;	// 是否有效
	
	protected static var _isStatic:Boolean;	// 是否为静态型(默认为动态型)
	
	/*cache*/

	protected static var _background:Panel;	// 背景面板
	
	protected static var _container:Sprite;	// 该容器一直在stage上显示、区别在有无显示内容
	
	protected static var _currTips:TipsItem;	// 显示状态

	protected static var _textField:TextField;
	
	protected static var _stage:Stage = SystemManager.m_stage;
	
	protected static var _currOwner:InteractiveObject;	
		
	protected static var _isClear:Boolean = true;	// 是否容器子元素已清空
	
	protected static var _color:uint = 0x2b1209;
	
	protected static var _lineColor:uint = 0xffffff;
	
	protected static var _thickness:Number = 1.3;
		
	protected static var _ellipse:Number = 8;
	
	protected static var _alpha:Number = 0.9;
	
	protected static var _gap:Number = 4;

	
	
	/**
	 * 初始化
	 * <TODO>必须调用！
     * @param isStatic		是否为静态型
	 **/
	public static function initialize(isStatic:Boolean, skin:IBackgroundSkin = null ):void
	{
		_isStatic = isStatic;
		
		// 背景面板
		_background = new Panel( -_gap, -_gap, new Point(1,1), skin);
		_background.visible = false;
		_background.mouseChildren = false;
		_stage.addChild(_background);
		
		// 容器
		_container = new Sprite();
		_container.mouseChildren = false;
		_container.mouseEnabled = false;
		_stage.addChild(_container);
	}
	
	/**
	 * 设置背景
     * @param bgColor		背景
     * @param lineColor		边线
     * @param thickness		粗细
	 * @param gap			背景边缘距离
	 **/
	public static function setBackground(color:uint = 0xc3F3F3, thickness:Number = 2, lineColor:uint = 0, ellipse:Number = 8, alpha:Number = 0.9 ):void
	{
		_color = color;
		_thickness = thickness;
		_lineColor = lineColor;
		_ellipse = ellipse;
		_alpha = alpha;
		_background.setBackground(color, thickness, lineColor, ellipse, alpha);
	}
	
	/**
	 * 添加tips
	 * @param owner			宿主容器
	 * @param data			数据 / 方法
	 */
	public static function add(owner:InteractiveObject,data:Object):void
	{
		_tipsProps[owner] = data;
		owner.addEventListener(MouseEvent.ROLL_OVER, ____onMouseOver, false, 0, true);
	}
	
	/**
	 * 删除tips
	 * @param owner			宿主容器
	 */
	public static function remove(owner:InteractiveObject):void
	{
		owner.removeEventListener(MouseEvent.ROLL_OVER, ____onMouseOver);
		delete _tipsProps[owner];
		if (_currOwner == owner)
		{
			____onMouseLeave();
		}
	}
	
	/**
	 * 更换tips
	 * @param tips			即时提示
	 */	
	public static function setTips(tips:TipsItem):void
	{
		clear();
		_currTips = tips;
		_currTips.enter();	
		_drawBackground();
	}
	
	/**
	 * 定点显示tips
	 * @param x				x轴坐标
	 * @param y				y轴坐标
	 * @param data			数据
	 */
	public static function showTipsAt(x:Number,y:Number,data:Object):void
	{
		clear();
		_parse(data);
		_adjustPosition(x,y);
	}
		
	/**
	 * 清除
	 **/
	public static function clear():void
	{
		if (_isClear)
		{
			return;
		}
		var len:int = _container.numChildren;
		for (var i:int = len - 1; i >= 0; i--)
		{
			_container.removeChildAt(i);	
		}
		
		//背景
		_background.visible = false;
		
		_isClear = true;
	}
	
	/**
	 * 毁灭
	 **/
	public static function destroy():void
	{
		if (_currOwner)
		{
			____onMouseLeave();
		}
		var temp:*= null;
		for (temp in _tipsProps)
		{
			remove(temp);
		}
		_tipsProps = null;
		_stage.removeChild(_container);
		_container = null;
		_stage = null;
	}
	
	
	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	

	
	// 显示容器
	public static function get container():Sprite
	{
		return _container;
	}
	
	// 当前宿主容器
	public function get owner():InteractiveObject
	{
		return _currOwner;
	}
	
	
	// 设置是否生效
	public static function set enabled(b:Boolean):void
	{
		if (_enabled == b)
		{
			return;
		}
		else
		{
			_enabled = b;
		}
		if (!_enabled && _currOwner)
		{
			____onMouseLeave();
		}
	}
	
	public static function get enabled():Boolean
	{
		return _enabled;
	}

	

	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	

	/**
	 * 滑鼠移进
	 **/
	private static function ____onMouseOver(e:MouseEvent):void
	{
		if (!_enabled) 
		{
			return;
		}
		
		_currOwner = e.currentTarget as InteractiveObject;
		_currOwner.addEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);
		
		// 静态tips
		if (_isStatic)
		{
			showTipsAt(_currOwner.x, _currOwner.y , _tipsProps[_currOwner] );
		}
		
		// 动态tips
		else
		{
			_parse();
			_currOwner.addEventListener(MouseEvent.MOUSE_MOVE, ____onMouseMove);
			// 位置
			_adjustPosition(_stage.mouseX,_stage.mouseY);
		}

	}
	
	/**
	 * 滑鼠移出
	 **/
	private static function ____onMouseLeave(e:MouseEvent = null):void
	{
		_currOwner.removeEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);
		if (!_isStatic)
		{
			_currOwner.removeEventListener(MouseEvent.MOUSE_MOVE, ____onMouseMove);
		}
		clear();
		_currOwner = null;
	}

	private static function ____onMouseMove(e:MouseEvent):void
	{
		_adjustPosition(_stage.mouseX,_stage.mouseY);
	}

	/**
	 * 解析当前宿主容器数据
	 **/
	private static function _parse(data:Object=null):void
	{
		//var data:Object = _tipsProps[_currOwner];
		//
		// 切换状态方法
		//if (data is Function)
		//{
			//var onShow:Function = _tipsProps[_currOwner];
			//onShow();
		//}
		//
		// HTML文本
		//else if (data is String)
		//{
			//_addText(String(data));
		//}
		_addText(String(data));
		// 绘制背景
		_drawBackground();
	}
	
	/**
	 * 添加文本
	 * @param content		文本内容
	 **/
	private static function _addText(content:String):void
	{
		if (!_textField)
		{
			_textField = DesT.createText(content);
		}
		else 
		{
			_textField.htmlText = content;
		}
		_container.addChild(_textField);
	}
	
	/**
	 * 调整位置
	 * @param x			x轴坐标
	 * @param y			y轴坐标
	 */
	private static function _adjustPosition(x:Number,y:Number):void
	{	
		// 容器
		var w:Number = _background.width;
		var h:Number = _background.height;
		var stageW:Number = _stage.stageWidth;
		_background.x = (stageW - x - w < 4)?x - w:x;
		_background.y = (y - h > 4)?y - h :y;
		
		// 背景
		_container.x = _background.x + _gap;
		_container.y = _background.y + _gap;
	}

	/**
	  * 绘制背景
	  */
	private static function _drawBackground():void
	{
		var w:Number = _container.width;
		var h:Number = _container.height;
		_background.setSize(w + _gap * 2, h + _gap * 2);
		_background.visible = true;
		_isClear = false;
	}
}
}